package com.hxl.aop.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.hxl.aop.res.AjaxResult;
import com.hxl.aop.res.RepeatSubmit;
import com.hxl.aop.res.annotation.Log;
import com.hxl.aop.res.aspect.LogAspect;
import com.hxl.aop.res.enums.BusinessType;
import com.hxl.aop.res.enums.OperatorType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * 防重复提交测试接口
 *
 * @author xy
 * @date 2024-06-05
 */
@RestController
@RequestMapping(path = "/repeatSubmit")
public class TestController {
    private Logger logger = LoggerFactory.getLogger(TestController.class);

    @RequestMapping("/test")
    @RepeatSubmit(lockedTime = 5, timeUnit = TimeUnit.SECONDS)
    public String test() {
        return "提交成功";
    }


    @Log(title = "测试AOP切面功能", businessType = BusinessType.OTHER, operatorType = OperatorType.MANAGE, isSaveRequestData = true)
    @PostMapping("/aop_test")
    public AjaxResult testAop(@RequestBody JsonNode jsonNode) {
        logger.info("请求进来了");
        CompletableFuture<Void> future=CompletableFuture.runAsync(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName()+"正在执行一个没有返回值的异步任务。");
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        try {
            future.get();
//            System.out.println(Thread.currentThread().getName() + " 1x:  " + result);
            TimeUnit.SECONDS.sleep(3);
            return AjaxResult.success();
            //            String s = completableFuture.get();
//            System.out.println(s);
        } catch (InterruptedException | ExecutionException exception) {
            exception.printStackTrace();
            return AjaxResult.error();
        }

    }
}
